package com.key.puzzlemodel.transverselongitudinal.fragment

import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.Toast
import com.key.puzzlemodel.R
import com.key.puzzlemodel.bean.HVPuzzleBean
import com.key.puzzlemodel.databinding.FragmentPuzzleEditBinding
import com.key.puzzlemodel.mvvm.BaseMVVMViewModel
import com.key.puzzlemodel.base.BaseMenuFragment
import com.key.puzzlemodel.transverselongitudinal.IPuzzleInterFace
import java.util.*
import kotlin.collections.ArrayList

/**
 *
 * @功能描述:     编辑
 * @创建时间:     2022/1/10
 * @创建者:       阳
 * @修改时间:     2022/1/10
 * @修改者:       阳
 */
class EditFragment :
    BaseMenuFragment<FragmentPuzzleEditBinding, BaseMVVMViewModel>(),
    View.OnClickListener {

    override val layoutId: Int
        get() = R.layout.fragment_puzzle_edit


    //临时缓存的列表图片
    var itemPhotoList = ArrayList<HVPuzzleBean>()
    var scrollIndex: Int = -1

    var editType = -1


    override fun onAttach(context: Context) {
        super.onAttach(context)
        puzzleInterface = context as IPuzzleInterFace
    }

    fun updateData(list: ArrayList<HVPuzzleBean>?, index: Int) {
        if (itemPhotoList.isNullOrEmpty() && null != list) {
            itemPhotoList = ArrayList<HVPuzzleBean>()
//            itemPhotoList.addAll(list)
            for (index in 0 until list.size) {
                itemPhotoList.add(list[index].copy())
            }
        }
        scrollIndex = index
    }


    /**
     * 图片位置交换
     */
    fun updateData(list: ArrayList<HVPuzzleBean>?, fromPosition: Int, toPosition: Int) {
        if (itemPhotoList.isNullOrEmpty() && null != list) {
            itemPhotoList = ArrayList<HVPuzzleBean>()
//            itemPhotoList.addAll(list)
            for (index in 0 until list.size) {
                itemPhotoList.add(list[index].copy())
            }
        }
        Collections.swap(itemPhotoList, fromPosition, toPosition)
        scrollIndex = toPosition
    }


    override fun initView(savedInstanceState: Bundle?) {
        mVDB.clickListener = this
    }

    fun updatePhotoList(photoUri: String) {
        if (editType == 0) { //替换
            if (scrollIndex == -1) {
                Toast.makeText(requireActivity(), "请选中需要图片", Toast.LENGTH_SHORT).show()
                return
            }
            itemPhotoList[scrollIndex].imagePath = photoUri

            puzzleInterface.clickEditItemShow(itemPhotoList, scrollIndex)
        } else if (editType == 1) {//添加

            var imgRound = itemPhotoList[0].imgRound

            if (scrollIndex == -1) {
                itemPhotoList.add(HVPuzzleBean(imagePath = photoUri, imgRound = imgRound))

                //没有值，在第一次添加后 增加赋值
                scrollIndex = itemPhotoList.size - 1
            } else {
                scrollIndex += 1

                itemPhotoList.add(
                    scrollIndex,
                    HVPuzzleBean(imagePath = photoUri, imgRound = imgRound)
                )
            }
            puzzleInterface.clickEditItemShow(itemPhotoList, scrollIndex)
        }
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.img_border_close -> {
                puzzleInterface.clickEditResult(null, scrollIndex)
                resetPhotoList()
            }
            R.id.img_border_ok -> {
                puzzleInterface.clickEditResult(itemPhotoList, scrollIndex)
            }
            R.id.tab_edit_replace -> { //替换
                if (scrollIndex == -1) {
                    Toast.makeText(requireActivity(), "请选中图片", Toast.LENGTH_SHORT).show()
                    return
                }
                editType = 0
                puzzleInterface.showPhotoFragment()

            }
            R.id.tab_edit_rotating -> { //旋转
//                puzzleInterface.clickEditPhoto(itemPhotoList[mVM.scrollIndex], mVM.scrollIndex, 1)
                if (scrollIndex == -1) {
                    Toast.makeText(requireActivity(), "请选中图片", Toast.LENGTH_SHORT).show()
                    return
                }

                var bean = itemPhotoList[scrollIndex]

                 bean.rotation = bean.rotation + 90

                if (bean.rotation >= 360) {
                    bean.rotation = bean.rotation - 360
                }

//                var width: Float = itemPhotoList[scrollIndex].imgWidth
//                var height: Float = itemPhotoList[scrollIndex].imgHeight

//                //缩放比例
//                var scale = if (bean.imgHeight >= bean.imgWidth) {
//                    bean.imgHeight / bean.imgWidth
//                } else {
//                    bean.imgWidth / bean.imgHeight
//                }
//                判断旋转的角度 是90或者270
//                if (bean.rotation == 90 || bean.rotation == 270) {
//                    bean.imgHeight =  bean.imgWidth * scale
//
//                    Log.e("图片坐标",  bean.imgWidth.toString() +"  旋转90度 " + itemPhotoList[scrollIndex].imgHeight);
//                } else {
//                    bean.imgHeight =  bean.imgWidth / scale
//                    Log.e("图片坐标",  bean.imgWidth.toString() +"  选转180度 " + itemPhotoList[scrollIndex].imgHeight);
//                }

                itemPhotoList[scrollIndex] = bean
0
                puzzleInterface.clickEditItemShow(itemPhotoList, scrollIndex)
            }
            R.id.tab_edit_delete -> { //删除
                if (scrollIndex == -1) {
                    Toast.makeText(requireActivity(), "请选中图片", Toast.LENGTH_SHORT).show()
                    return
                }
                if (itemPhotoList.size <= 1) {
                    Toast.makeText(requireActivity(), "至少有一个图片", Toast.LENGTH_SHORT).show()
                    return
                }

                itemPhotoList.removeAt(scrollIndex)

                scrollIndex -= 1
                //越界处理
                if (scrollIndex <= 0) {
                    scrollIndex = 0
                }

                puzzleInterface.clickEditItemShow(itemPhotoList, scrollIndex)
            }
            R.id.tab__edit_add -> { //添加
                if (itemPhotoList.size >= 20) {
                    Toast.makeText(requireActivity(), "最多可添加20张图片", Toast.LENGTH_SHORT).show()
                    return
                }
                editType = 1
                puzzleInterface.showPhotoFragment()
            }
        }
    }


    /**
     * 重置 缓存列表
     */
    private fun resetPhotoList() {
        itemPhotoList.clear()
        //itemPhotoList.addAll(mVM.photoList)
    }

}